﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALCardType
    /// </summary>
    public partial class DALCardType
    {
        public DALCardType()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbCardType");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbCardType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbCardType(");
            strSql.Append("name,description,photoUrl,isEnable,professionAreaTypeId,shopId,sortOrder,createTime,creater)");
            strSql.Append(" values (");
            strSql.Append("@name,@description,@photoUrl,@isEnable,@professionAreaTypeId,@shopId,@sortOrder,@createTime,@creater)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,500),
					new SqlParameter("@photoUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@isEnable", SqlDbType.Bit,1),
					new SqlParameter("@professionAreaTypeId", SqlDbType.Int,4),
					new SqlParameter("@shopId", SqlDbType.Int,4),
					new SqlParameter("@sortOrder", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@creater", SqlDbType.Int,4)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.description;
            parameters[2].Value = model.photoUrl;
            parameters[3].Value = model.isEnable;
            parameters[4].Value = model.professionAreaTypeId;
            parameters[5].Value = model.shopId;
            parameters[6].Value = model.sortOrder;
            parameters[7].Value = model.createTime;
            parameters[8].Value = model.creater;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbCardType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbCardType set ");
            strSql.Append("name=@name,");
            strSql.Append("description=@description,");
            strSql.Append("photoUrl=@photoUrl,");
            strSql.Append("isEnable=@isEnable,");
            strSql.Append("professionAreaTypeId=@professionAreaTypeId,");
            strSql.Append("shopId=@shopId,");
            strSql.Append("sortOrder=@sortOrder,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("creater=@creater");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,500),
					new SqlParameter("@photoUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@isEnable", SqlDbType.Bit,1),
					new SqlParameter("@professionAreaTypeId", SqlDbType.Int,4),
					new SqlParameter("@shopId", SqlDbType.Int,4),
					new SqlParameter("@sortOrder", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.description;
            parameters[2].Value = model.photoUrl;
            parameters[3].Value = model.isEnable;
            parameters[4].Value = model.professionAreaTypeId;
            parameters[5].Value = model.shopId;
            parameters[6].Value = model.sortOrder;
            parameters[7].Value = model.createTime;
            parameters[8].Value = model.creater;
            parameters[9].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbCardType ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbCardType ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


         /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbCardType GetModelByName(string sCardType) 
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,name,description,photoUrl,isEnable,professionAreaTypeId,shopId,sortOrder,createTime,creater from tbCardType ");
            strSql.Append(" where [name]=@name");
            SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.NVarChar,50)
			};
            parameters[0].Value = sCardType;

            Gift.Model.tbCardType model = new Gift.Model.tbCardType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["description"] != null && ds.Tables[0].Rows[0]["description"].ToString() != "")
                {
                    model.description = ds.Tables[0].Rows[0]["description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["photoUrl"] != null && ds.Tables[0].Rows[0]["photoUrl"].ToString() != "")
                {
                    model.photoUrl = ds.Tables[0].Rows[0]["photoUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isEnable"] != null && ds.Tables[0].Rows[0]["isEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["isEnable"].ToString().ToLower() == "true"))
                    {
                        model.isEnable = true;
                    }
                    else
                    {
                        model.isEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["professionAreaTypeId"] != null && ds.Tables[0].Rows[0]["professionAreaTypeId"].ToString() != "")
                {
                    model.professionAreaTypeId = int.Parse(ds.Tables[0].Rows[0]["professionAreaTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["shopId"] != null && ds.Tables[0].Rows[0]["shopId"].ToString() != "")
                {
                    model.shopId = int.Parse(ds.Tables[0].Rows[0]["shopId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sortOrder"] != null && ds.Tables[0].Rows[0]["sortOrder"].ToString() != "")
                {
                    model.sortOrder = int.Parse(ds.Tables[0].Rows[0]["sortOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["creater"] != null && ds.Tables[0].Rows[0]["creater"].ToString() != "")
                {
                    model.creater = int.Parse(ds.Tables[0].Rows[0]["creater"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbCardType GetModel(int Id) 
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,name,description,photoUrl,isEnable,professionAreaTypeId,shopId,sortOrder,createTime,creater from tbCardType ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Gift.Model.tbCardType model = new Gift.Model.tbCardType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["description"] != null && ds.Tables[0].Rows[0]["description"].ToString() != "")
                {
                    model.description = ds.Tables[0].Rows[0]["description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["photoUrl"] != null && ds.Tables[0].Rows[0]["photoUrl"].ToString() != "")
                {
                    model.photoUrl = ds.Tables[0].Rows[0]["photoUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isEnable"] != null && ds.Tables[0].Rows[0]["isEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["isEnable"].ToString().ToLower() == "true"))
                    {
                        model.isEnable = true;
                    }
                    else
                    {
                        model.isEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["professionAreaTypeId"] != null && ds.Tables[0].Rows[0]["professionAreaTypeId"].ToString() != "")
                {
                    model.professionAreaTypeId = int.Parse(ds.Tables[0].Rows[0]["professionAreaTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["shopId"] != null && ds.Tables[0].Rows[0]["shopId"].ToString() != "")
                {
                    model.shopId = int.Parse(ds.Tables[0].Rows[0]["shopId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sortOrder"] != null && ds.Tables[0].Rows[0]["sortOrder"].ToString() != "")
                {
                    model.sortOrder = int.Parse(ds.Tables[0].Rows[0]["sortOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["creater"] != null && ds.Tables[0].Rows[0]["creater"].ToString() != "")
                {
                    model.creater = int.Parse(ds.Tables[0].Rows[0]["creater"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,name,description,photoUrl,isEnable,professionAreaTypeId,shopId,sortOrder,createTime,creater ");
            strSql.Append(" FROM tbCardType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,name,description,photoUrl,isEnable,professionAreaTypeId,shopId,sortOrder,createTime,creater ");
            strSql.Append(" FROM tbCardType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbCardType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from tbCardType T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbCardType";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

